package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.CategoryMapper;
import org.csu.mypetstore.persistence.ItemMapper;
import org.csu.mypetstore.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatalogService {

      @Autowired
      private CategoryMapper categoryMapper;

      @Autowired
      private ProductMapper productMapper;

      @Autowired
      private ItemMapper itemMapper;


      public List<Category> getCategoryList() {
            return categoryMapper.getCategoryList();
      }

      public Category getCategory(String categoryId) {
            return categoryMapper.getCategory(categoryId);
      }

      public Product getProduct(String productId) {
            return productMapper.getProduct(productId);
      }

      public List<Product> getProductListByCategory(String categoryId) {
            return productMapper.getProductListByCategory(categoryId);
      }

      // TODO enable using more than one keyword
      public List<Product> searchProductList(String keyword) {
            return productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
      }

      public List<Item> getItemListByProduct(String productId) {
            return itemMapper.getItemListByProduct(productId);
      }

      public Item getItem(String itemId) {
            return itemMapper.getItem(itemId);
      }

      public boolean isItemInStock(String itemId) {
            return itemMapper.getInventoryQuantity(itemId) > 0;
      }


      //查询
      public List<Category> selectCategory(String catId) {
            return categoryMapper.selectCategory("%" + catId + "%");
      }

      //更新
      public void updateCategory(Category category) {

            categoryMapper.updateCategory(category);
      }


      public void insertCategory(Category category) {
            categoryMapper.insertCategory(category);
      }

      public void deleteCategory(String catId) {
            List<Product> productList = productMapper.getProductListByCategory(catId);
            for (int i = 0; i < productList.size(); i++) {
                  String productId = productList.get(i).getProductId();
                  System.out.println(productId);
                  productMapper.deleteProductItem(productId);
                  productMapper.deleteProduct(productId);
            }
            categoryMapper.deleteCategory(catId);
      }


      //更新Product
      public void updateProduct(Product product) {
            productMapper.updateProduct(product);
      }


      //插入Product
      public void insertProduct(Product product) {
            productMapper.insertProduct(product);
      }

      public List<Product> getAllProducts(String categoryId) {
            return productMapper.selectProduct("%" + categoryId + "%");
      }

      public void deleteProduct(String productId) {
            productMapper.deleteProductItem(productId);
            productMapper.deleteProduct(productId);
      }

      public void insertItem(Item item) {
            itemMapper.insertItem(item);
      }

      public List<Item> getAllItems(String productId) {
            return itemMapper.selectItem("%" + productId + "%");
      }

      public void updateItem(Item item) {
            itemMapper.updateItem(item);
      }

      public void deleteItem(String itemId) {
            itemMapper.deleteItem(itemId);
      }


      public void searchProductList() {
      }

      public void upConfirmItem(String itemId){
            itemMapper.upConfirmItem(itemId);
      }

      public void upCancelItem(String itemId){
            itemMapper.upCancelItem(itemId);
      }
}
